package com.sxt.system.service.impl;

import com.sxt.system.domain.Notice;
import com.sxt.system.mapper.NoticeMapper;
import com.sxt.system.service.INoticeService;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.vo.NoticeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-09
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

	private Log log = LogFactory.getLog(NoticeServiceImpl.class);
	
	@Autowired
	private NoticeMapper noticeMapper;
	
	/**
	 * 	分页查询系统公告
	 */
	@Override
	@Transactional(readOnly = true)
	public DataGridView loadAllNotice(NoticeVO noticeVO) {
		QueryWrapper<Notice> qw = new QueryWrapper<Notice>();
		if(null != noticeVO) {
			qw.like(StringUtils.isNoneBlank(noticeVO.getTitle()), "title", noticeVO.getTitle());
			qw.ge(noticeVO.getStartTime() != null, "createtime", noticeVO.getStartTime());
			qw.le(noticeVO.getEndTime() != null, "createtime", noticeVO.getEndTime());
		}else {
			log.info("noticeVO为空");
		}
		qw.orderByDesc("createtime");
		Page<Notice> page = new Page<Notice>(noticeVO.getPage(), noticeVO.getLimit());
		this.noticeMapper.selectPage(page, qw);
		
		return new DataGridView(page.getTotal(),page.getRecords());
	}

	@Override
	public void addNotice(Notice notice) {
		this.noticeMapper.insert(notice);
	}

	@Override
	public void updateNotice(Notice notice) {
		this.noticeMapper.updateById(notice);
	}

}
